<?php
class Admin_Model_Tintuc extends Zend_Db_Table_Abstract{
    protected $_name='table_news';
    protected $_primary='id';
    protected $_sequence=true;    
    
    public function getAll(){                
        $se=$this->select(SELECT_WITH_FROM_PART);
        $se->setIntegrityCheck(false);
                
        $ta_news_item=new Admin_Model_Tintucitem;
        $cols=$ta_news_item->info(Zend_Db_Table_Abstract::COLS);        
        $news_item_cols=array();
        foreach($cols as $item){            
            $news_item_cols[]=$item.' as news_item_'.$item;
        }
        
        $ta_news_cat=new Admin_Model_Tintuccat;        
        $cols=$ta_news_cat->info(Zend_Db_Table_Abstract::COLS);        
        $news_cat_cols=array();
        foreach($cols as $item){            
            $news_cat_cols[]=$item.' as news_cat_'.$item;
        }        
                
        $se->join(array('i'=>'table_news_item'),
        'table_news.id_item=i.id',
        $news_item_cols);        
        
        $se->join(array('c'=>'table_news_cat'),
        'i.id_cat=c.id',
        $news_cat_cols);                                        
                
        if($kq=$this->fetchAll($se))
            return $kq->toArray();
        else
            return false;                                 
    }
    
    public function getWhere($id){
        $se=$this->select();
        $se->where("id=$id");        
        if($kq=$this->fetchRow($se))
            return $kq->toArray();
        else
            return false;  
    }
    
    public function getaArrWhere($arr_search){
        //print_r($arr_search);die();
        $se=$this->select(SELECT_WITH_FROM_PART);
        $se->setIntegrityCheck(false);
                
        $ta_news_item=new Admin_Model_Tintucitem;
        $cols=$ta_news_item->info(Zend_Db_Table_Abstract::COLS);        
        $news_item_cols=array();
        foreach($cols as $item){            
            $news_item_cols[]=$item.' as news_item_'.$item;
        }
        
        $ta_news_cat=new Admin_Model_Tintuccat;
        $cols=$ta_news_cat->info(Zend_Db_Table_Abstract::COLS);        
        $news_cat_cols=array();
        foreach($cols as $item){            
            $news_cat_cols[]=$item.' as news_cat_'.$item;
        }        
                
        $se->join(array('i'=>'table_news_item'),
        'table_news.id_item=i.id',
        $news_item_cols);        
        
        $se->join(array('c'=>'table_news_cat'),
        'i.id_cat=c.id',
        $news_cat_cols);                                              
               
        if(isset($arr_search[ten])){            
            $se->where("table_news.ten like '%$arr_search[ten]%'");                                     
        }          
        if(isset($arr_search[danhmuc1]) && $arr_search[danhmuc1]!=0){            
            $se->where("c.id=$arr_search[danhmuc1]");            
        }
        if(isset($arr_search[danhmuc2]) && $arr_search[danhmuc2]!=0){
            $se->where("i.id=$arr_search[danhmuc2]");            
        }                                         
                                                                      
        if($kq=$this->fetchAll($se))
            
            return $kq->toArray();
        else
            return false;   
    }
    
    public function updateTin($data,$id){
        $this->update($data,"id=$id");          
    }
    
    public function delTin($id){        
        $this->delete("id=$id");          
    }
    
    
    
}

